package com.ws.haungjia.controller;

import com.ws.haungjia.Bean.Fenlie;
import com.ws.haungjia.Bean.Lunbo;
import com.ws.haungjia.Bean.User;
import com.ws.haungjia.Bean.luxian;
import com.ws.haungjia.Service.DengluService;
import com.ws.haungjia.Service.FenlieService;
import com.ws.haungjia.Service.LvxingService;
import com.ws.haungjia.utils.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/qianduan")
public class Dengluzhuce {
    @Autowired
    private DengluService dengluService;
    @RequestMapping("/zhuce")
    public String add(User user, MultipartFile image) throws IOException {

        System.out.println(user);
        if (image.getSize() > 0) {
            String replace = UUID.randomUUID().toString().replace("-", "");
            String fileName = replace + "." + image.getOriginalFilename().split("\\.")[1];
            user.setTouxiang(fileName);

            image.transferTo(new File(Res.fileDir + fileName));
        }
        String s = DigestUtils.md5DigestAsHex(user.getPass().getBytes());
        User chachong = dengluService.chachong(user.getName());
        System.out.println(chachong);
        if (chachong != null) {
            if (chachong.getName().equals(user.getName())) {
                return "WEB-INF/qianduan/zhuce";
            }
        }else {
            user.setPass(s);
            int add = dengluService.addUser(user.getName(), user.getPass(), user.getShouji(), user.getTouxiang(), new Date());
            if (add > 0) {
                return "WEB-INF/qianduan/denglu";
            }
        }
        return "WEB-INF/qianduan/zhuce";
        }


    @RequestMapping("/denglu")
    public String denglu(String name, String pass, HttpServletRequest request) throws IOException {
        String s = DigestUtils.md5DigestAsHex(pass.getBytes());
        User denglu = dengluService.denglu(name, s);
        System.out.println(denglu);
        if (denglu!=null){
            request.getSession().setAttribute("user", denglu);
            return "redirect:/qianduan/chaxun";

        }else{
            return "WEB-INF/qianduan/denglu";
        }
    }
    @RequestMapping("/tuichu")
    public String tuichu(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return "index";
    }


    @Autowired
    FenlieService fenlieService;

    @Autowired
    LvxingService lvxingService;
    @RequestMapping("/chaxun")
    public String qianhou(Model model) {
        List<luxian> qiancha = lvxingService.qiancha();
        List<Fenlie> chaxun = fenlieService.chaxun();
        List<Lunbo> luncha = lvxingService.luncha();
        model.addAttribute("luxians",qiancha);
        model.addAttribute("lunbos",luncha);
        model.addAttribute("fenlie",chaxun);
        return "index";
    }
}

